package com.esincrunch.tool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;


public class SMSDetail extends Activity{
	 public String val1;
	 public String val2;
	 public String val3;
	 public String val4;
	 public TextView tv1;
	 public TextView tv2;
	 public TextView tv3;
	 public TextView tv4;
	 public String carrier;
	 private static final String TAG = "SMSDetail";
	 SharedPreferences sp;
	 
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	        setContentView(R.layout.smsdetail);       
	 }
	 @Override
	 protected void onResume() {
		 super.onResume();
		 sp = PreferenceManager.getDefaultSharedPreferences(this);
	     carrier=sp.getString("carrier_choice", "");
	     Log.e(TAG, carrier);
	     getData();
	 }
	 private void getData(){
		 Intent intent=getIntent();
	     val1=intent.getStringExtra("id");
		 val2=intent.getStringExtra("serviceno");
		 val3=intent.getStringExtra("sendtime");
		 val4=intent.getStringExtra("sendtext");
	     tv1 = (TextView) this.findViewById(R.id.tv_sms_id);
	     tv2 = (TextView) this.findViewById(R.id.tv_sms_addr);
	     tv3 = (TextView) this.findViewById(R.id.tv_sms_date);
	     tv4 = (TextView) this.findViewById(R.id.tv_sms_body);
	     tv1.setText(val1);
	     tv2.setText(val2);
	     tv3.setText(val3);
	     tv4.setText(val4);
	 }
	 
	 @Override
	 public boolean onCreateOptionsMenu(Menu menu) {
	        MenuItem menu_sms_nospam=menu.add(0,0,0,getString(R.string.sd_menu_nospam));
	        MenuItem menu_sms_send=menu.add(0,0,1,getString(R.string.sd_menu_send));

	        menu_sms_nospam.setIcon(android.R.drawable.ic_menu_report_image);
	        menu_sms_send.setIcon(android.R.drawable.ic_menu_send);
	        
	        menu_sms_nospam.setOnMenuItemClickListener(new OnMenuItemClickListener(){
	            public boolean onMenuItemClick(MenuItem item) {
	            	exeNoSpam();
	                return true;
	            }
	        });
	        menu_sms_send.setOnMenuItemClickListener(new OnMenuItemClickListener(){
	            public boolean onMenuItemClick(MenuItem item) {
	            	sendMessageToCarrier();
	                return true;
	            }
	        });
	        menu_sms_send.setAlphabeticShortcut('o');
	        return true; 
	 }
	 private void exeNoSpam(){
		 ContentValues cv = new ContentValues();
		 Uri mUri=Uri.parse(ListProvider.CONTENT_URI +"/"+ val1);
		 cv.put(ListProvider.TYPENO, "1");
		 getContentResolver().update(mUri, cv, null, null);
		 /*
		  * insert sms to inbox
		  */
		 Long mDate=null;
	     SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
	     try {
	    	 mDate=sdf.parse(val3).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		ContentValues cv1 = new ContentValues();
		 //cv.put("_id", "0");    
		 //cv.put("thread_id", "0");    
		 cv1.put("address", val2);    
		 //cv.put("person", "0");    
		 cv1.put("date", mDate); 
		 //cv.put("protocol", "0"); 
		 cv1.put("read", "1"); 
		 //cv.put("status", "-1"); 
		 //cv.put("type", "0"); 
		 cv1.put("body", val4); 
		 getContentResolver().insert(Uri.parse("content://sms/inbox"), cv1); 
	 }
	 
	private void sendMessageToCarrier(){
		String maddress =carrier;
		String mbody=tv4.getText().toString();
		if(maddress.length()==0){
			Toast.makeText(SMSDetail.this,getString(R.string.sd_menu_carrier_null_tip)+"("+carrier+")",Toast.LENGTH_LONG).show();
			Intent intent =new Intent();
            intent.setClass(SMSDetail.this, Preferences.class);
            SMSDetail.this.startActivity(intent);
		}
		else{
			if(carrier.equals("10086999")){
				mbody = tv2.getText()+"*"+tv4.getText();
			}
			else if(carrier.equals("10010")){
				mbody = "JB "+tv4.getText();
			}
			SmsManager sms = SmsManager.getDefault();
			//mbody = sms.divideMessage(mbody).toString();
			PendingIntent pi = PendingIntent.getBroadcast(SMSDetail.this,0,new Intent(),0); 
			if(mbody.length()>70){
				ArrayList<String> Lbody=sms.divideMessage(mbody);
				
				for (String content : Lbody){
					sms.sendTextMessage(maddress,null,content,pi,null);
					Toast.makeText(SMSDetail.this,getString(R.string.sd_menu_send_ok_tip)+"("+carrier+")",Toast.LENGTH_LONG).show();
					Log.e(TAG, content);
				}
			}
			else{
				sms.sendTextMessage(maddress,null,mbody,pi,null);
				Toast.makeText(SMSDetail.this,getString(R.string.sd_menu_send_ok_tip)+"("+carrier+")",Toast.LENGTH_LONG).show();
				Log.e(TAG, mbody);
				
			}
		}
		
		
		
		
	}
	 
	    
		

}
